package com.wedimob.slideshare.source;

import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import com.wedimob.slideshare.KeyWordType;
import com.wedimob.slideshare.SearchCriteria;

public class ScribdStrategy implements SourceStrategy {

    public final static String URL_BASE = "http://fr.scribd.com";
    public final static String SEARCH_PATTERN = URL_BASE + "/search";
    private final static double NB_ITEM_PER_PAGE = 10.0;

    @Override
    public String getUrlBase() {
        return URL_BASE;
    }

    @Override
    public List<URL> generateUrl(SearchCriteria criteria) {
        try {
            List<URL> result = new ArrayList<URL>();
            int nbPages = Source.getNbPages(criteria.getLimit(), NB_ITEM_PER_PAGE);
            int begin = (int) Math.floor(criteria.getBegin() / NB_ITEM_PER_PAGE);
            for (int i = begin; i < begin + nbPages; i++) {
                result.add(new URL(URL_BASE
                        + (
                        criteria.getKeyWordType() == KeyWordType.CONTENT ?
                                "/search?category=&filetype=&language=&num_pages=&paid=false&uploaded_on=&"
                                : "/search-people")
                        + "?limit=10&page=" + (i + 1)
                        + "&query=" + criteria.getKeyword().replaceAll(" ", "+")));
            }
            return result;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
